//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;
using DotNetNuke.UI.WebControls;
namespace DotNetNuke.Modules.Admin.Security
{
	partial class Roles : Entities.Modules.PortalModuleBase, Entities.Modules.IActionable
	{
		private int RoleGroupId = -1;
		private void BindData()
		{
			RoleController objRoles = new RoleController();
			ArrayList arrRoles;
			if (RoleGroupId < -1)
			{
				arrRoles = objRoles.GetPortalRoles(PortalId);
			}
			else
			{
				arrRoles = objRoles.GetRolesByGroup(PortalId, RoleGroupId);
			}
			grdRoles.DataSource = arrRoles;
			if (RoleGroupId < 0)
			{
				lnkEditGroup.Visible = false;
				cmdDelete.Visible = false;
			}
			else
			{
				lnkEditGroup.Visible = true;
				lnkEditGroup.NavigateUrl = EditUrl("RoleGroupId", RoleGroupId.ToString(), "EditGroup");
				cmdDelete.Visible = arrRoles.Count == 0;
				ClientAPI.AddButtonConfirm(cmdDelete, Services.Localization.Localization.GetString("DeleteItem"));
			}
			DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref grdRoles, this.LocalResourceFile);
			grdRoles.DataBind();
		}
		private void BindGroups()
		{
			ListItem liItem;
			ArrayList arrGroups = RoleController.GetRoleGroups(PortalId);
			if (arrGroups.Count > 0)
			{
				cboRoleGroups.Items.Clear();
				cboRoleGroups.Items.Add(new ListItem(Localization.GetString("AllRoles"), "-2"));
				liItem = new ListItem(Localization.GetString("GlobalRoles"), "-1");
				if (RoleGroupId < 0)
				{
					liItem.Selected = true;
				}
				cboRoleGroups.Items.Add(liItem);
				foreach (RoleGroupInfo roleGroup in arrGroups)
				{
					liItem = new ListItem(roleGroup.RoleGroupName, roleGroup.RoleGroupID.ToString());
					if (RoleGroupId == roleGroup.RoleGroupID)
					{
						liItem.Selected = true;
					}
					cboRoleGroups.Items.Add(liItem);
				}
				trGroups.Visible = true;
			}
			else
			{
				RoleGroupId = -2;
				trGroups.Visible = false;
			}
			BindData();
		}
		public string FormatPeriod(int period)
		{
			string _FormatPeriod = Null.NullString;
			try
			{
				if (period != Null.NullInteger)
				{
					_FormatPeriod = period.ToString();
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
			return _FormatPeriod;
		}
		public string FormatPrice(float price)
		{
			string _FormatPrice = Null.NullString;
			try
			{
				if (price != Null.NullSingle)
				{
					_FormatPrice = price.ToString("##0.00");
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
			return _FormatPrice;
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			foreach (DataGridColumn column in grdRoles.Columns)
			{
				if (object.ReferenceEquals(column.GetType(), typeof(ImageCommandColumn)))
				{
					ImageCommandColumn imageColumn = (ImageCommandColumn)column;
					imageColumn.Visible = this.IsEditable;
					if (imageColumn.CommandName == "Delete")
					{
						imageColumn.OnClickJS = Localization.GetString("DeleteItem");
					}
					if (imageColumn.CommandName == "Edit")
					{
						string formatString = EditUrl("RoleID", "KEYFIELD", "Edit");
						formatString = formatString.Replace("KEYFIELD", "{0}");
						imageColumn.NavigateURLFormatString = formatString;
					}
					if (imageColumn.CommandName == "UserRoles")
					{
						string formatString = Common.Globals.NavigateURL(TabId, "User Roles", "RoleId=KEYFIELD", "mid=" + ModuleId.ToString());
						formatString = formatString.Replace("KEYFIELD", "{0}");
						imageColumn.NavigateURLFormatString = formatString;
					}
					if (!String.IsNullOrEmpty(imageColumn.CommandName))
					{
						imageColumn.Text = Localization.GetString(imageColumn.CommandName, this.LocalResourceFile);
					}
				}
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cboRoleGroups.SelectedIndexChanged += new EventHandler(cboRoleGroups_SelectedIndexChanged);
			cmdDelete.Click += new System.Web.UI.ImageClickEventHandler(cmdDelete_Click);
			grdRoles.ItemDataBound += new DataGridItemEventHandler(grdRoles_ItemDataBound);

			try
			{
				if (!Page.IsPostBack)
				{
					if ((Request.QueryString["RoleGroupID"] != null))
					{
						RoleGroupId = Int32.Parse(Request.QueryString["RoleGroupID"]);
					}
					BindGroups();
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cboRoleGroups_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			RoleGroupId = Int32.Parse(cboRoleGroups.SelectedValue);
			BindData();
		}
		private void cmdDelete_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			RoleGroupId = Int32.Parse(cboRoleGroups.SelectedValue);
			if (RoleGroupId > -1)
			{
				RoleController.DeleteRoleGroup(PortalId, RoleGroupId);
				RoleGroupId = -1;
			}
			BindGroups();
		}
		private void grdRoles_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			DataGridItem item = e.Item;
			if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.SelectedItem)
			{
				System.Web.UI.Control imgColumnControl = item.Controls[0].Controls[0];
				if (imgColumnControl is HyperLink)
				{
					HyperLink editLink = (HyperLink)imgColumnControl;
					RoleInfo role = (RoleInfo)item.DataItem;
					editLink.Visible = role.RoleName != PortalSettings.AdministratorRoleName || (PortalSecurity.IsInRole(PortalSettings.AdministratorRoleName));
				}
				imgColumnControl = item.Controls[1].Controls[0];
				if (imgColumnControl is HyperLink)
				{
					HyperLink rolesLink = (HyperLink)imgColumnControl;
					RoleInfo role = (RoleInfo)item.DataItem;
					rolesLink.Visible = role.RoleName != PortalSettings.AdministratorRoleName || (PortalSecurity.IsInRole(PortalSettings.AdministratorRoleName));
				}
			}
		}
		public ModuleActionCollection ModuleActions
		{
			get
			{
				ModuleActionCollection Actions = new ModuleActionCollection();
				Actions.Add(GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, LocalResourceFile), ModuleActionType.AddContent, "", "add.gif", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
				Actions.Add(GetNextActionID(), Localization.GetString("AddGroup.Action", LocalResourceFile), ModuleActionType.AddContent, "", "add.gif", EditUrl("EditGroup"), false, SecurityAccessLevel.Edit, true, false);
				Actions.Add(GetNextActionID(), Localization.GetString("UserSettings.Action", LocalResourceFile), ModuleActionType.AddContent, "", "settings.gif", EditUrl("UserSettings"), false, SecurityAccessLevel.Edit, true, false);
				return Actions;
			}
		}
	}
}
